﻿/** \file
* \$Rev: 187 $
* 
* \$Date: 2010-04-27 21:58:51 +0000 (Tue, 27 Apr 2010) $
*
* \$URL: http://chorus-twitter.googlecode.com/svn/trunk/src/utils/TwitterJSConverter.cs $
*/

using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using chorus.twitter;

namespace chorus.utils
{
	/// <summary>
	/// The converter class responsible for the serialization and
	/// deserialization of all custom types required for JSON
	/// transactions with Twitter.
	/// </summary>
	public class TwitterJSConverter : JavaScriptConverter
	{
		/// <summary>The list of types that this converter supports.</summary>
		public override IEnumerable<Type> SupportedTypes
		{
			get
			{
				return new Type[]
				{
					typeof(Dictionary<DateTime, List<QueryTrend>>),
					typeof(AbstractSerializable)
				};
			}
		}

		/// <summary>
		/// Deserialize the given dictionary into an object.
		/// </summary>
		/// <param name="dictionary">The JSON dictionary.</param>
		/// <param name="type">The type of the returned deserialized object.</param>
		/// <param name="serializer">The associated serializer object.</param>
		/// <returns>The deserialized object.</returns>
		public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
		{
			if (type == typeof(Dictionary<DateTime, List<QueryTrend>>))
			{
				Dictionary<DateTime, List<QueryTrend>> trenddict = new Dictionary<DateTime, List<QueryTrend>>();
				foreach (KeyValuePair<string, object> kvp in dictionary)
					trenddict[DateTime.Parse(kvp.Key)] =
						serializer.ConvertToType<List<QueryTrend>>(kvp.Value);
				return trenddict;
			}
			if (type == typeof(AbstractSerializable))
			{

			}
			return null;
		}

		/// <summary>
		/// Serialize the given object into a JSON dictionary.
		/// </summary>
		/// <param name="obj">The object to serialize.</param>
		/// <param name="serializer">The associated serializer.</param>
		/// <returns>The serialized JSON dictionary.</returns>
		public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
		{
			Dictionary<string, object> dict = new Dictionary<string, object>();
			return dict;
		}
	}
}
